package com.ruoyi.web.controller.system;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * 单点登录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/sso")
public class SysLoginSsoController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysUserService userService;

    @PostMapping("/loginSso")
    public AjaxResult loginSso(String token) {
        if (StringUtils.isBlank(token)) {
            return AjaxResult.error("传递的token为空，验证失败!");
        }

        //获取用户
        String url = "http://59.203.29.53:8800/stweb/uapUser/checkTokenId?tokenId=" + token;
        //String url = "https://zhhb.iflyeco.com/stweb/uapUser/checkTokenId?tokenId=" + token;
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
        if (response.getStatusCode() == HttpStatus.REQUEST_TIMEOUT){
            return AjaxResult.error(15408,"请求超时，请稍后再试");
        }
        if (response.getStatusCode() == HttpStatus.OK){
            JSONObject jsonObject = JSONObject.parseObject(response.getBody());
            Boolean flag = jsonObject.getBoolean("flag");
            if(!flag){
                throw new RuntimeException("获取用户信息失败");
            }
            //组装用户信息
            JSONObject dataObject = jsonObject.getJSONObject("data");
            String userName = dataObject.getString("loginName");
            String nickName = dataObject.getString("loginName");
            String phone = dataObject.getString("phone");

            SysUser sysUser = new SysUser();
            sysUser.setUserName(userName);
            sysUser.setNickName(nickName);
            sysUser.setPhonenumber(phone);
            sysUser.setPassword(SecurityUtils.encryptPassword("yqhs@2026"));
            sysUser.setCreateBy("sso");
            sysUser.setCreateTime(new Date());

            //检查用户是否存在，不存在则新建
            if (userService.checkUserNameUnique(sysUser)) {
                //Long[] roleIds={2L};
                //sysUser.setRoleIds(roleIds);
                userService.insertUser(sysUser);
            }

            AjaxResult ajax = AjaxResult.success();
            String tokenNew = loginService.loginSso(sysUser.getUserName());
            ajax.put(Constants.TOKEN, tokenNew);
            return ajax;
        }
        return AjaxResult.error("获取用户信息失败");
    }

}
